package com.aaa.service.impl;

import com.aaa.dao.DictDataDao;
import com.aaa.dao.DictTypeDataDao;
import com.aaa.entity.DictData;
import com.aaa.entity.DictType;
import com.aaa.service.DictDataService;
import com.aaa.util.AccountName;
import com.aaa.util.WebGetUser;
import com.aaa.vo.DictDataVo;
import com.aaa.vo.Result;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.xml.crypto.Data;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @BelongsPackage: com.aaa.service.impl
 * @Description:
 * @CreateTime: 2023-03-22  19:15
 * @Author: ql
 */
@Service
public class DictDataServiceImpl implements DictDataService {

    @Autowired
    private DictDataDao dictDataDao;

    @Autowired
    private DictTypeDataDao dictTypeDataDao;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public Result<List<DictData>> findByType(String type) {
        try{
            String s = redisTemplate.opsForValue().get(type);
            if(StringUtils.hasText(s)){
                List<DictData> dictData = JSON.parseArray(s, DictData.class);
                System.out.println("redis查询");
                return new Result<>(2000,"查询成功",dictData);

            }
        }catch (Exception e){
            e.printStackTrace();
        }
        QueryWrapper<DictData> wrapper=new QueryWrapper<>();
        wrapper.eq("dict_type",type);
        List<DictData> list = dictDataDao.selectList(wrapper);
        return new Result<>(2000,"查询成功",list);
    }

    @Override
    public List<DictData> listType(String type) {
        QueryWrapper<DictData> wrapper=new QueryWrapper<>();
        wrapper.eq("dict_type",type);
        return dictDataDao.selectList(wrapper);
    }

    @Override
    public Result<IPage<DictData>> queryDictByPage(DictDataVo dictDataVo) {
        IPage<DictData> page = new Page<>(dictDataVo.getPageNum(),dictDataVo.getPageSize());
        QueryWrapper<DictData> wrapper = new QueryWrapper<>();

//        wrapper.eq("status",0);

        if (StringUtils.hasText(dictDataVo.getDictLabel())){
            wrapper.like("dict_label",dictDataVo.getDictLabel());
        }

        if (StringUtils.hasText(dictDataVo.getDictType())){
            wrapper.like("dict_type",dictDataVo.getDictType());
        }
        if (dictDataVo.getStatus()!=null){
            wrapper.eq("status",dictDataVo.getStatus());
        }

        IPage<DictData> dictDataPage1 = dictDataDao.selectPage(page, wrapper);

        return new Result<>(2000,"susses",dictDataPage1);
    }

    @Override
    public Result<String> cacheAsync() {
        QueryWrapper<DictType> wrapper = new QueryWrapper<>();
        wrapper.eq("status",0);
        List<DictType> dictTypes = dictTypeDataDao.selectList(wrapper);
        for (DictType dictType : dictTypes) {
            //查询所有数据字典的数据
            QueryWrapper<DictData> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("status",0);
            wrapper1.eq("dict_type",dictType.getDictType());
            List<DictData> dictData = dictDataDao.selectList(wrapper1);
            redisTemplate.opsForValue().set(dictType.getDictType(), JSON.toJSONString(dictData));
        }
        return new  Result<>(2000,"缓存同步成功");
    }

    @Override
    public Result<DictData> getDictDataById(Long id) {

        DictData dictData = dictDataDao.selectById(id);
        return new Result<>(2000,"ok",dictData);
    }

    @Override
    public Result<String> deleteDictDataByIds(Long[] id) {
        List<Long> collect = Arrays.stream(id).collect(Collectors.toList());
        System.out.println("===========多选删除id==============="+collect);
        int i = dictDataDao.deleteBatchIds(collect);
        if (i>0){
            return Result.success();
        }
        return Result.error();
    }

    @Autowired
    private AccountName accountName;

    @Override
    public Result<String> addOrUpdDictData(DictData dictData) {

        String userName = accountName.getAccountName();
        int num;
        if (dictData.getDictCode()!=null){
            //执行修改
            num = dictDataDao.updateById(dictData);
            dictData.setUpdateBy(userName);
            dictData.setUpdateTime(new Date());
        }else {

            dictData.setCreateBy(userName);
            dictData.setCreateTime(new Date());
            num = dictDataDao.insert(dictData);
            //执行添加
        }
        if (num>0){
            return Result.success();
        }else {
            return Result.error();
        }
    }
}
